/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

/**
 *
 * @author Antonio
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "reparto.cercaPerNome", query = "SELECT r FROM Reparto r WHERE r.nome=?1"),
    @NamedQuery(name = "reparto.cercaPerId", query = "SELECT r FROM Reparto r WHERE r.id=?1")})
@NamedNativeQueries({
    @NamedNativeQuery(name = "reparto.repartiProdottiFornitiDalNegozio",
    query = "SELECT * FROM REPARTO r WHERE EXISTS( SELECT p.codiceBarre FROM "
    + "PRODOTTO p WHERE p.REPARTO_ID=r.id AND (p.codiceBarre)IN(SELECT f.prodotto FROM FORNITURA f WHERE f.negozio=?1 AND f.quantita>0))",
    resultClass = Reparto.class)
})
public class Reparto implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(unique = true)
    private String nome;
    @OneToMany(mappedBy = "reparto")
    private List<Prodotto> prodotti;

    /**
     * Get the value of prodotti
     *
     * @return the value of prodotti
     */
    public List<Prodotto> getProdotti() {
        return prodotti;
    }

    /**
     * Set the value of prodotti
     *
     * @param prodotti new value of prodotti
     */
    public void setProdotti(List<Prodotto> prodotti) {
        this.prodotti = prodotti;
    }

    /**
     * Get the value of nome
     *
     * @return the value of nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * Set the value of nome
     *
     * @param nome new value of nome
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Reparto)) {
            return false;
        }
        Reparto other = (Reparto) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return id + "," + nome;
    }
}
